package com.wish.controller;


import com.wish.entity.*;
import com.wish.service.*;
import com.wish.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 闫东浩
 * @since 2021-03-29
 */
@RestController
@RequestMapping("/house-info")
public class HouseInfoController {

    @Autowired
    IHouseInfoService houseInfoService;
    @Autowired
    IRoomInfoService roomInfoService;
    @Autowired
    IHouseStyleService houseStyleService;
    @Autowired
    IRelationHousePictureService relationHousePictureService;
    @Autowired
    IRelationRoomPictureService relationRoomPictureService;

    @RequestMapping("add")
    public ResultEntity add(@RequestBody HouseInfoDto houseInfoDto){

        boolean flag=true;
        Integer premisesId = houseInfoDto.getPremisesId();
        String flatName = houseInfoDto.getFlatName();
        Integer[] publicFacility = houseInfoDto.getPublicFacility();
        String id="";

        for (Integer ids:publicFacility){
            id=ids+","+id;
        }

        if (id.length()>=1){
            id.substring(1);
        }
        Integer paymentPay= houseInfoDto.getPaymentPay();
        Integer paymentMortgage = houseInfoDto.getPaymentMortgage();
        Integer decorate =houseInfoDto.getDecorate();
        Integer room =houseInfoDto.getRoom();
        Integer hall =houseInfoDto.getHall();
        Integer manger =houseInfoDto.getManager();
        String introduce = houseInfoDto.getIntroduce();
        List<HouseStyleDto> houseStyles = houseInfoDto.getHouseStyles();


        HouseInfo houseInfo=new HouseInfo();
        //添加主表
        String zuid= UUID.randomUUID()+"";
        houseInfo.setId(zuid);
        houseInfo.setPremisesId(premisesId);
        houseInfo.setFlatName(flatName);
        houseInfo.setPublicFacility(id);
        houseInfo.setPaymentPay(paymentPay);
        houseInfo.setPaymentMortgage(paymentMortgage);
        houseInfo.setDecorate(decorate);
        houseInfo.setRoom(room);
        houseInfo.setHall(hall);
        houseInfo.setManager(manger);
        houseInfo.setIntroduce(introduce);
        boolean insert = houseInfoService.save(houseInfo);
        System.out.println("insert+++++++++++++++++++++++++++"+insert);
        if(insert!=true){
            flag=false;
        }
        //添加子表
        for(HouseStyleDto hou:houseStyles){
            if(hou==null){
                continue;
            }

            String acreage = hou.getAcreage();
            Double proportion = hou.getProportion();
            Integer direction = hou.getDirection();
            Double rental = hou.getRental();
            Integer[] privateFacility = hou.getPrivateFacility();
            String id1="";

            for (Integer ids:privateFacility){
                id1=ids+","+id1;
            }

            if (id1.length()>=1){
                id1.substring(1);
            }
            HouseStyle house=new HouseStyle();
            house.setId(UUID.randomUUID()+"");
            house.setHouseId(zuid);
            house.setAcreage(acreage);
            house.setProportion(proportion);
            house.setDirection(direction);
            house.setRental(rental);
            house.setPrivateFacility(id1);
            boolean insert1 = houseStyleService.save(house);
            if(insert1!=true){
                flag=false;
            }
        }
        return ResultEntity.ok(flag);

    }

    @RequestMapping("/insert")
    public ResultEntity insert( HouseInfo HouseInfo){

        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        HouseInfo.setCreateTime(now);
        System.out.println(HouseInfo);
        boolean save = houseInfoService.save(HouseInfo);
        System.out.println(save);
        HouseInfo houseinfo = new HouseInfo();
        //TbRoomInfo表
        RoomInfo ti = new RoomInfo();
        //添加room inif表
        if (HouseInfo.getRental() != null){
            ti.setHouseId(HouseInfo.getId());
            System.out.println(HouseInfo.getId());
            ti.setRental(HouseInfo.getRental());
            ti.setPrivateFacility(HouseInfo.getPrivateFacility());
            boolean save1 = roomInfoService.save(ti);
            System.out.println(save1);
        }
        //添加TbHouseStyle
        List<HouseStyle> styleList = HouseInfo.getStyleList();
        if(styleList!=null){
            for (HouseStyle ts : styleList){
                System.out.println(ts);
                ts.setHouseId(HouseInfo.getId());

                boolean save1 = houseStyleService.save(ts);
                System.out.println("添加TbHouseStyle成功!"+save1);
            }

        }

        //TbRelationHousePicture表
        RelationHousePicture rhp = new RelationHousePicture();
        //判断公共区域照片
        if (HouseInfo.getImgHone() != null && HouseInfo.getImgHone() != ""){
            rhp.setHouseId(HouseInfo.getId());
            rhp.setPictureUrl(HouseInfo.getImgHone());
            boolean save1 = relationHousePictureService.save(rhp);
            System.out.println(save1);
        }
        //TbRelationRoomPicture表
        RelationRoomPicture rrp = new RelationRoomPicture();
        //判断房间区域照片
        if (HouseInfo.getImgRoom() != null && HouseInfo.getImgRoom() != ""){
            rrp.setRoomId(ti.getId());
            System.out.println(ti.getId());
            rrp.setPictureUrl(HouseInfo.getImgRoom());
            final boolean save1 = relationRoomPictureService.save(rrp);
            System.out.println(save1);
        }

        if (save){
            return ResultEntity.ok();
        }
        return null;
    }

}
